<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-us" lang="en-us">
<head>
  <link href="//gmpg.org/xfn/11" rel="profile">
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta name="generator" content="Hugo 0.68.3" />

  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Python基础1（列表、元组、内置函数） &middot; 我的博客</title>

  
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/print.css" media="print">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/poole.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/syntax.css">
  <link type="text/css" rel="stylesheet" href="/my_technology_blog/css/hyde.css">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abril+Fatface|PT+Sans:400,400i,700">


  
  <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/apple-touch-icon-144-precomposed.png">
  <link rel="shortcut icon" href="/favicon.png">

  
  
</head>

  <body class="theme-base-0b ">
  <aside class="sidebar">
  <div class="container sidebar-sticky">
    <div class="sidebar-about">
      <a href="/my_technology_blog/"><h1>我的博客</h1></a>
      <p class="lead">
       杨博的博客 
      </p>
    </div>

    <nav>
      <ul class="sidebar-nav">
        <li><a href="/my_technology_blog/">Home</a> </li>
        
      </ul>
    </nav>

    <p>&copy; 2020. All rights reserved. </p>
  </div>
</aside>

    <main class="content container">
    <div class="post">
  <h1>Python基础1（列表、元组、内置函数）</h1>
  <time datetime=2020-06-22T17:00:27&#43;0800 class="post-date">Mon, Jun 22, 2020</time>
  <h2 id="列表和元组">列表和元组</h2>
<h3 id="列表">列表</h3>
<blockquote>
<h4 id="list">list</h4>
<p>可迭代类型 可变类型 有序类型</p>
<p>使用方括号来创建列表</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">li <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#34;a&#34;</span>, <span style="color:#e6db74">&#34;b&#34;</span>, <span style="color:#e6db74">&#34;c&#34;</span>, <span style="color:#e6db74">&#34;d&#34;</span>]
</code></pre></div></blockquote>
<h4 id="索引和切片">索引和切片</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span>li[<span style="color:#ae81ff">0</span>]<span style="color:#e6db74">&#39;a&#39;</span><span style="color:#f92672">&gt;&gt;&gt;</span>li[<span style="color:#ae81ff">1</span>:<span style="color:#ae81ff">2</span>][<span style="color:#e6db74">&#39;b&#39;</span>]
</code></pre></div><p>与字符串切片功能不同之处：</p>
<ul>
<li>进行获取、切片、赋值给其他变量等 （getitem）得到的是其浅拷贝</li>
<li>进行直接赋值（setitem）则会改变原列表</li>
</ul>
<h4 id="getitem操作">getitem操作</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>]a[:<span style="color:#ae81ff">1</span>][<span style="color:#ae81ff">0</span>] <span style="color:#f92672">=</span> <span style="color:#ae81ff">5</span><span style="color:#66d9ef">print</span>(a)<span style="color:#75715e">#打印结果[1,2]</span>
</code></pre></div><h4 id="setitem操作">setitem操作</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>]a[:<span style="color:#ae81ff">1</span>] <span style="color:#f92672">=</span> [<span style="color:#ae81ff">5</span>]<span style="color:#66d9ef">print</span>(a)<span style="color:#75715e">#打印结果[5,2]</span>
</code></pre></div><p>关于切片</p>
<p>可以在学习面向对象的魔术方法后再深入研究</p>
<h4 id="操作符">操作符</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">[]<span style="color:#f92672">+</span>[]  <span style="color:#75715e">#拼合[]*int  #重复item in [] #判断成员是否存在 返回布尔值</span>
</code></pre></div><h4 id="常用内置函数">常用内置函数</h4>
<ul>
<li><code>len(list)</code> 获取长度</li>
<li><code>list()</code> 转化为列表类型</li>
<li><code>max()</code>/<code>min()</code>求最值，可选参数key进行操作</li>
<li><code>reversed(list)</code>临时翻转，不限于列表</li>
<li><code>sorted(iterable)</code>临时排序，不限于列表 ，可选key和倒序。</li>
<li><code>sum</code>所有成员是数字的时候可用，求和，不限于列表</li>
</ul>
<h4 id="常用方法">常用方法</h4>
<ul>
<li><code>''.join(list)</code> 将列表转为字符串</li>
<li><code>.count(value)</code>计次</li>
<li><code>.copy()</code>浅拷贝，和切片<code>[:]</code>等效</li>
</ul>
<h5 id="增删改查">增删改查</h5>
<ul>
<li><code>.append(p)</code> 追加</li>
<li><code>.extend(iterable)</code>迭代追加</li>
<li><code>.insert(index,obj)</code>插入</li>
<li><code>.pop(index=-1)</code> 删除默认最后一个 并返回它的值</li>
<li><code>.remove(value)</code> 删除</li>
<li><code>del</code> 删除 索引/切片</li>
<li><code>.clear()</code> 清空</li>
<li><code>.index(value)</code>查询是否存在,不存在会报错</li>
</ul>
<h5 id="排序">排序</h5>
<p><code>.sort()</code>永久排序
<code>.reverse()</code>倒序，相当于<code>li[:] = li[::-1]</code>的效果</p>
<p>.sort()即list.sort(self)方法，和全局函数sorted(iterable)</p>
<p>它们本质是<strong>快排算法</strong>两两比较大小
它们的参数完全一致（sorted不限于列表）。不同的是：
列表的sort方法无返回值，直接修改self
内置函数sorted，返回临时排序后的列表</p>
<h3 id="成员赋值">成员赋值</h3>
<h4 id="逐个赋值">逐个赋值</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> arr <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>]<span style="color:#f92672">&gt;&gt;&gt;</span> a, b, c <span style="color:#f92672">=</span> arr<span style="color:#f92672">&gt;&gt;&gt;</span> a1<span style="color:#f92672">&gt;&gt;&gt;</span> b2<span style="color:#f92672">&gt;&gt;&gt;</span> c3
</code></pre></div><h4 id="获取剩余">获取剩余</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> arr1 <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>, <span style="color:#ae81ff">6</span>, <span style="color:#ae81ff">7</span>, <span style="color:#ae81ff">8</span>, <span style="color:#ae81ff">9</span>]<span style="color:#f92672">&gt;&gt;&gt;</span> c, <span style="color:#f92672">*</span>d, e <span style="color:#f92672">=</span> arr1<span style="color:#f92672">&gt;&gt;&gt;</span> c1<span style="color:#f92672">&gt;&gt;&gt;</span> d[<span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>, <span style="color:#ae81ff">6</span>, <span style="color:#ae81ff">7</span>, <span style="color:#ae81ff">8</span>]<span style="color:#f92672">&gt;&gt;&gt;</span> e9<span style="color:#f92672">&gt;&gt;&gt;</span>
</code></pre></div><p>此方法不限于列表</p>
<h3 id="元组">元组</h3>
<blockquote>
<h4 id="httpnanarinometoyuncompy5-列表和元组htmltupletuple"><a href="http://nanarino.metoyun.com/py/5-%E5%88%97%E8%A1%A8%E5%92%8C%E5%85%83%E7%BB%84.html#tuple">#</a>tuple</h4>
<p>可迭代类型 不可变类型 有序类型</p>
<p>元组中只包含一个元素时，必须在末尾元素后面添加逗号 （推荐所有情况都加逗号）
创建元组使用小括号</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">tup <span style="color:#f92672">=</span> (<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>,)
</code></pre></div></blockquote>
<h4 id="索引和切片-1">索引和切片</h4>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span>tup[<span style="color:#ae81ff">1</span>]<span style="color:#ae81ff">2</span><span style="color:#f92672">&gt;&gt;&gt;</span>tup[:<span style="color:#ae81ff">1</span>](<span style="color:#ae81ff">1</span>,)
</code></pre></div><p>和字符串的索引和切片操作几乎一样。
元组的切片和索引<strong>只读</strong>（只有getitem操作）。</p>
<p>内置函数<code>tuple()</code>可以将对象转化为元组</p>
<h4 id="常用方法-1">常用方法</h4>
<ul>
<li><code>.count()</code></li>
<li><code>.index()</code></li>
</ul>
<p>对象方法是返回新的对象还是修改对象本身</p>
<p>一般来说，对于可变类型 对象方法如<code>list.sort()</code>会影响到self本身
而对于不可变类型，对象方法如<code>str.replace()</code>会返回一个预期的新对象</p>
<h3 id="内置函数enumerate">内置函数enumerate()</h3>
<p><code>for i in 列表</code> 只能遍历出列表成员，如果需要同时遍历出索引可以使用：
<code>enumerate(sequence, [start=0])</code> 意为枚举，将一个可迭代的对象，同时列出数据和数据下标
可以用list()函数把enumerate()返回的枚举对象转为一个列表</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span>li <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#34;a&#34;</span>, <span style="color:#e6db74">&#34;b&#34;</span>, <span style="color:#e6db74">&#34;c&#34;</span>]<span style="color:#f92672">&gt;&gt;&gt;</span>list(enumerate(li))[(<span style="color:#ae81ff">0</span>, <span style="color:#e6db74">&#39;a&#39;</span>), (<span style="color:#ae81ff">1</span>, <span style="color:#e6db74">&#39;b&#39;</span>), (<span style="color:#ae81ff">2</span>, <span style="color:#e6db74">&#39;c&#39;</span>)]
</code></pre></div><p>一般直接在for循环中使用</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">namelist <span style="color:#f92672">=</span> [<span style="color:#e6db74">&#39;夏色祭&#39;</span>,<span style="color:#e6db74">&#39;赤井心&#39;</span>,<span style="color:#e6db74">&#39;湊阿夸&#39;</span>]<span style="color:#66d9ef">for</span> i, name <span style="color:#f92672">in</span> enumerate(namelist):	<span style="color:#66d9ef">print</span>(i,name)<span style="color:#e6db74">&#39;&#39;&#39;打印内容0 夏色祭1 赤井心2 湊阿夸&#39;&#39;&#39;</span>
</code></pre></div><h3 id="内置函数slice">内置函数slice()</h3>
<p>意为切片
<code>slice(stop)</code>
<code>slice(start, stop[, step])</code></p>
<p>实例化一个切片对象，用来代替切片中的中括号内的内容</p>
<ul>
<li>start – 起始位置</li>
<li>stop – 结束位置</li>
<li>step – 间距</li>
</ul>
<p>切片对象用索引的写法<code>[slice(1,2)]</code>可以达到切片的效果<code>[1:2]</code></p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#f92672">&gt;&gt;&gt;</span> a <span style="color:#f92672">=</span> slice(<span style="color:#ae81ff">10</span>)<span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#66d9ef">print</span>(a)slice(None, <span style="color:#ae81ff">10</span>, None)<span style="color:#f92672">&gt;&gt;&gt;</span> a <span style="color:#f92672">=</span> slice(<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">10</span>,<span style="color:#ae81ff">2</span>)<span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#66d9ef">print</span>(a)slice(<span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">10</span>, <span style="color:#ae81ff">2</span>)<span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>start)<span style="color:#ae81ff">1</span><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>stop)<span style="color:#ae81ff">10</span><span style="color:#f92672">&gt;&gt;&gt;</span> <span style="color:#66d9ef">print</span>(a<span style="color:#f92672">.</span>step)<span style="color:#ae81ff">2</span><span style="color:#f92672">&gt;&gt;&gt;</span> li <span style="color:#f92672">=</span> [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">6</span>,<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">8</span>,<span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">0</span>]<span style="color:#f92672">&gt;&gt;&gt;</span> li[slice(<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">6</span>)][<span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>, <span style="color:#ae81ff">5</span>, <span style="color:#ae81ff">6</span>]
</code></pre></div><h3 id="内置函数range">内置函数range()</h3>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">range(stop)<span style="color:#e6db74">``</span>range(start, stop[, step])
</code></pre></div><p>参数类似切片，可创建一个整数列表，可以很方便的用在for循环中，
一般用来固定循环for次数，与其他语言中的`for(i=0; i效果相似</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python"><span style="color:#e6db74">&#34;&#34;&#34;Python3.x 中 range() 函数返回的结果是一个整数序列的可迭代对象,而不是列表可以用list()函数把range()返回的可迭代对象转为一个列表 &#34;&#34;&#34;</span><span style="color:#f92672">&gt;&gt;&gt;</span> list(range(<span style="color:#ae81ff">5</span>))[<span style="color:#ae81ff">0</span>, <span style="color:#ae81ff">1</span>, <span style="color:#ae81ff">2</span>, <span style="color:#ae81ff">3</span>, <span style="color:#ae81ff">4</span>]
</code></pre></div><p>打印一个九九乘法表，来试试吧，例如：</p>
<div class="highlight"><pre style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-python" data-lang="python">a<span style="color:#f92672">=</span><span style="color:#e6db74">&#34;&#34;</span><span style="color:#66d9ef">for</span> i <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">10</span>):    <span style="color:#66d9ef">for</span> j <span style="color:#f92672">in</span> range(<span style="color:#ae81ff">1</span>,i<span style="color:#f92672">+</span><span style="color:#ae81ff">1</span>):        a<span style="color:#f92672">+=</span>(str(j)<span style="color:#f92672">+</span><span style="color:#e6db74">&#34;×&#34;</span><span style="color:#f92672">+</span>str(i)<span style="color:#f92672">+</span><span style="color:#e6db74">&#34;=&#34;</span><span style="color:#f92672">+</span>str(j<span style="color:#f92672">*</span>i)<span style="color:#f92672">+</span><span style="color:#e6db74">&#34; &#34;</span>)    a<span style="color:#f92672">+=</span><span style="color:#e6db74">&#34;</span><span style="color:#ae81ff">\n</span><span style="color:#e6db74">&#34;</span>   <span style="color:#66d9ef">print</span>(a)
</code></pre></div>
</div>


    </main>

    
  </body>
</html>
